package org.yoao.crm.page.framework;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yxwonline.com
 * @description 分页默认抽象实现
 * 初始时，分页类有下列默认值：
 * 分页大小为-1,为不分页;
 * 总页数为1页
 * 当前页为第一页
 * 总记录数为0条
 * 当前页数据列表为没有任何记录的列表
 * @date 2010-7-31 下午04:25:07
 */
public abstract class AbstractViewPage implements IViewPageService {
	//-----------------------------------------
	//私有静态常量
	//----------------------------------------- 

	private static final int DEFAULT_PAGE_INDEX = 1;

	private static final int DEFALT_PAGE_COUNT = 1;

	private static final int DEFAULT_PAGE_SIZE = -1;

	private static final int DEFAULT_ROWS = 0;

	//-----------------------------------------
	//私有成员变量
	//----------------------------------------- 

	/**当前页索引号**/
	private int pageIndex = DEFAULT_PAGE_INDEX;
	/**总页数**/
	private int pageCount =DEFALT_PAGE_COUNT;

	/**分页大小**/
	private int pageSize = DEFAULT_PAGE_SIZE ;

	/**数据总记录数**/
	private int rows = DEFAULT_ROWS;


	//------------------------------------------
	//本地成员变量getter,setter方法
	//------------------------------------------ 


	/**
	* @description 设置新页号，只有大于等于1而且小于等于总页数并且不为当前页时，才允许设置
	* @param pageIndex The pageIndex to set.
	*/
	private void setPageIndex(int newPageIndex){
		if( newPageIndex >= AbstractViewPage.DEFAULT_PAGE_INDEX && newPageIndex 
				<= this.getPageCount() && newPageIndex != this.pageIndex){
			this.pageIndex = newPageIndex;
		}
	}

	/**
	* @return Returns the rows.
	*/
	private int getRows(){
		return rows;
	}

	/**
	* @param rows The rows to set.
	*/
	private void setRows(int rows){
		this.rows = rows;
	}
	
	/**
	* @param pageCount The pageCount to set.
	*/
	private void setPageCount(int pageCount){
		this.pageCount = pageCount;
	}



	//--------------------------------------
	//实现Page接口方法
	//-------------------------------------- 

	public List getPageData(Map whereMap){
		List pageList = null;
	
		//获得当前页数据 
		pageList = this.pageList(this.getPageFirstRecord(), this.getPageRows(), whereMap);
		//保证不返回null 
		if(pageList == null){
			pageList = new ArrayList();
		}
		return pageList;
	}
	
	public List getPageData(){
		List pageList = null;
	
		//获得当前页数据 
		pageList = this.pageList(this.getPageFirstRecord(), this.getPageRows(), null);
		//保证不返回null 
		if(pageList == null){
			pageList = new ArrayList();
		}
		return pageList;
	}
	
	public int getPageIndex(){
		return this.pageIndex;
	}

	public boolean isFirstPage(){
		return AbstractViewPage.DEFAULT_PAGE_INDEX == this.pageIndex;
	}
	
	public boolean isLastPage(){
		return this.pageIndex == this.pageCount; //当前页索引为总页数时为最后一页 
	}

	public boolean getHashNextPage(){
		return this.pageIndex < this.pageCount; //当前页索引号小于总页数 
	}

	public boolean getHashPreviousPage(){
		//当前页索引号大于默认的初始页号,这里为1 
		return this.pageIndex > AbstractViewPage.DEFAULT_PAGE_INDEX ;
	}


	public int getPageCount(){
		return this.pageCount;
	}

	public int getPageSize(){
		return this.pageSize;
	}

	public int getPageRows(){
		//当页面大小为-1 时,返回总记录数 
		if(AbstractViewPage.DEFAULT_PAGE_SIZE == this.pageSize ){
			return this.rows;
		}

		//不为最后一页时，返回pageSize 
		if(!this.isLastPage()){
			return this.pageSize;
		}
		//最后一页时 
		return this.rows - (this.pageSize * (this.pageCount -1));
	}

	public int getPageFirstRecord(){
		//页大小为-1 时 
		if(AbstractViewPage.DEFAULT_PAGE_SIZE == this.pageSize ){
			return 0;
		}
		return (this.pageIndex -1)* this.pageSize;
	}

	public int getPageLastRecord(){
		//页大小为-1时,返回总记录数 
		if(AbstractViewPage.DEFAULT_PAGE_SIZE == this.pageSize){
			return this.rows;
		}
		return this.getPageFirstRecord() + this.getPageRows();
	}
	
	public void gotoFirstPage(){
		this.gotoPage(AbstractViewPage.DEFAULT_PAGE_INDEX);
	}

	public void gotoLastPage(){
		this.gotoPage(this.getPageCount());
	}

	public void gotoPreviousPage(){
		this.gotoPage(this.getPageIndex() -1);
	}

	public void gotoNextPage(){
		this.gotoPage(this.getPageIndex() + 1);
	}

	public void gotoPage(int newPageIndex){
		if( newPageIndex >= AbstractViewPage.DEFAULT_PAGE_INDEX && newPageIndex
				<= this.getPageCount() ){
			this.setPageIndex(newPageIndex);
		}
	}
	
	public void setPageSize(int size, Map whereMap){
		if(size < 1){
			size = 1;
		}
		this.pageSize = size;
		//进行初始化 
		this.doInit(whereMap);
	}
	
	public int getTotalRows(){
		return rows;
	}

	//-----------------------------------
	//辅助方法
	//----------------------------------- 


	/**
	* @description 分页初始化方法,为了保证总是能正确的初始化，所以声明为final ,为了让子类可以调用声明为protected
	*
	*/
	protected final void doInit(Map whereMap){
		int rows = 0;
	
		//获得总记录数 
		rows = totalRows(whereMap);
	
		//设置总记录数 
		this.setRows(rows);
		//设置新的总页数
	
		//计算并设置总页数 
		int pages = calculatePageCount();
		this.setPageCount(pages);
	
		//转到第一页 
		this.gotoPage(AbstractViewPage.DEFAULT_PAGE_INDEX);
	
		onInit();
	}
	
	/**
	* @description 记算总页数
	* @return 总页数
	*/
	private int calculatePageCount(){
		//总记录数为0条，则返回的总页数为1 
		if(this.getRows() == 0){
			return AbstractViewPage.DEFALT_PAGE_COUNT;
		}
	
		//如果页面大小为-1,则返回的总页数为1 
		if(AbstractViewPage.DEFAULT_PAGE_SIZE == this.getPageSize() ){
			return AbstractViewPage.DEFALT_PAGE_COUNT;
		}
	
		return this.getRows() / this.getPageSize() + ( this.getRows()
				% this.getPageSize() == 0 ? 0 :1);
	}


	/**
	* @description 获得总记录数，调用queryTotalRows(),将异常封装为non-checked 异常
	* @return 总记录数
	* @throws RuntimeException
	*/
	private int totalRows(Map whereMap) throws RuntimeException{
		try{
			return queryTotalRows(whereMap);
		}
		catch(Exception ex){
			throw new RuntimeException(ex);
		}
	}
	/**
	* @description 获得当前页数据，调用queryPageList()方法，将异常封装为non-checked异常
	* @param startRow 开始记录号
	* @param rowCount 记录总数
	* @return 当前页数据
	* @throws RuntimeException
	*/
	private List pageList(int startRow, int rowCount, Map whereMap) throws RuntimeException{
		try{
			return queryPageList(startRow, rowCount, whereMap);
		}catch(Exception ex){
			throw new RuntimeException(ex);
		}
	}


	//-----------------------------------------
	//子类实现的方法
	//----------------------------------------- 

	/**
	* @description 初始化附加方法，由子类扩展
	*/
	protected void onInit(){

	}


	/**
	* @description 查询获得总记录数，由子类具体实现
	* @return 总记录数
	* @throws Exception
	*/
	protected abstract int queryTotalRows(Map whereMap) throws Exception;

	/**
	* @description 查询当前页数据,从startRow 开始的rowCount条记录
	* @param startRow 开始记录号
	* @param rowCount 记录总数
	* @return 当前页数据
	* @throws Exception
	*/
	protected abstract List queryPageList(int startRow, int rowCount, Map whereMap) throws Exception;
}
